package com.ymatou.api.marketingseller.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.api.marketingseller.service.CancelOrEndPromotionCall;
import com.ymatou.api.marketingseller.service.CreateSellerPromotionCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtSellerPromotion;
import com.ymttest.database.mongo.SellerPromotionInfoWapper;
import com.ymttest.database.sqlwapper.YmtSellerPromotionIMapper;
import com.ymttest.utils.YMTDateUtil;


/**
 * @since 2016-12-08 10:16:14
 * @author chencan
 *
 */
public class Ts_CancelOrEndPromotion {
	private static CancelOrEndPromotionCall cancelOrEndPromotionCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("业务说明：取消或者提前结束促销活动");
	}

	@Before
	public void caseUp() {
		cancelOrEndPromotionCall = new CancelOrEndPromotionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cancelOrEndPromotionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_001() {
		Logger.start(true, "提前结束促销活动");
		try {
			// 创建一个活动,并结束该活动
			this.checkIsExistPromotion();
			System.out.println("aaa");
			// 设置活动时间
			Map<String,Object> PromotionMap = new HashMap<String,Object>();
			PromotionMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(0));
			PromotionMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(PromotionMap);
			
			// 促销商品ID
			int  promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 买手ID
			int sellerId = retBea.getJSONObject("JsonBeanRequest").getInt("SellerId");
			
			//更新活动状态
			String promotionBeginTime = YMTDateUtil.getBeforeOrNextDay(-1);
			int a = new  YmtSellerPromotionIMapper().updateStateByPromotionId(promotionId,promotionBeginTime);
			
			//SQL查询
			List<YmtSellerPromotion> PromotionList = new YmtSellerPromotionIMapper().selectPromotionByPromotionId(promotionId);
			YmtSellerPromotion ymtPromotion = PromotionList.get(0);
			int promotion = ymtPromotion.getPromotionState();
			
			//当活动状态等于1时，程序继续
			if (promotion == 1){
				System.out.println("promotion");
			}else{
				return;
			}
				
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",sellerId);
			map1.put("PromotionId",promotionId);
			map1.put("OperatorType",2);
			
			
			// 取消或者提前结束促销活动
			JSONObject retBea1 = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			Integer promotionId1 = retBea1.getJSONObject("JsonBeanRequest").getInt("PromotionId"); // 促销ID
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			int code =retBea.getInt("Code"); // 响应Code
			
			// 查询SQL促销数据
			List<YmtSellerPromotion> sellerPromotionList = new YmtSellerPromotionIMapper().selectPromotionByPromotionId(promotionId1);
			YmtSellerPromotion ymtSellerPromotion = sellerPromotionList.get(0);
			int state = ymtSellerPromotion.getPromotionState(); // 获取活动状态
			
			// 获取mongo sellerpromotion库的满减促销数据
			Map<String, Object> mongoDataMap = new SellerPromotionInfoWapper().getPromotionId(promotionId1);
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(true, flag, "验证促销活动是否成功取消");
			Logger.verifyEquals(4, state, "promotionstate是否等于4");
			Logger.verifyEquals(null, mongoDataMap, "mongo数据是否为空");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_002() {
		Logger.start(true, "取消促销活动");
		try {
			this.checkIsExistPromotion();
			Map<String,Object> PromotionMap = new HashMap<String,Object>();
			PromotionMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(-1));
			PromotionMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(PromotionMap);
			// 促销商品ID
			int  promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 买手ID
			int SellerId = retBea.getJSONObject("JsonBeanRequest").getInt("SellerId");
			
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",SellerId);
			map1.put("PromotionId",promotionId);
			map1.put("OperatorType",1);

			// 取消或者提前结束促销活动
			JSONObject retBea1 = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			boolean flag = retBea1.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			int code =retBea1.getInt("Code"); // 响应Code

			
			// 查询促销数据
			List<YmtSellerPromotion> sellerPromotionList = new YmtSellerPromotionIMapper().selectPromotionByPromotionId(promotionId);
			YmtSellerPromotion ymtSellerPromotion = sellerPromotionList.get(0);
			int state = ymtSellerPromotion.getPromotionState(); // 获取活动状态

			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(true, flag, "验证促销活动是否成功取消");
			Logger.verifyEquals(3, state, "promotionstate是否等于3");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_003() {
		Logger.start(true, "取消促销活动失败，活动已结束");
		try {
			
			//请求参数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",512006);
			map1.put("PromotionId",16);
			map1.put("OperatorType",1);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			int code =retBea.getInt("Code"); // 响应Code
			

			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动取消失败", msg, "促销活动取消失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_0031() {
		Logger.start(true, "取消促销活动失败，活动已取消");
		try {
			
			//请求参数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",598283);
			map1.put("PromotionId",13);
			map1.put("OperatorType",1);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			int code =retBea.getInt("Code"); // 响应Code
			

			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动取消失败", msg, "促销活动取消失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_0032() {
		Logger.start(true, "取消促销活动失败，活动已开始");
		try {
			
			List<YmtSellerPromotion> sellerPromotionIDlist = new YmtSellerPromotionIMapper().selectPromotionByPromotionState(3383);
			YmtSellerPromotion promotionstate =sellerPromotionIDlist.get(0);
			int sellerId = promotionstate.getSellerId();
			int promotionId = promotionstate.getPromotionId();
			
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",sellerId);
			map1.put("PromotionId",promotionId);
			map1.put("OperatorType",1);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			int code =retBea.getInt("Code"); // 响应Code
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动取消失败", msg, "促销活动取消失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_0033() {
		Logger.start(true, "取消促销活动失败，活动不存在");
		try {
			
			//请求参数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",3383);
			map1.put("PromotionId",99999);
			map1.put("OperatorType",1);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			int code =retBea.getInt("Code"); // 响应Code
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动取消失败", msg, "促销活动取消失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_004() {
		Logger.start(true, "提前结束促销活动失败，活动已结束");
		try {
			
			//请求参数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",512006);
			map1.put("PromotionId",16);
			map1.put("OperatorType",2);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			int code =retBea.getInt("Code"); // 响应Code
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动提前结束失败", msg, "促销活动提前结束失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_0041() {
		Logger.start(true, "提前结束促销活动失败，活动未开始");
		try {
			
			// 创建一个活动,并结束该活动
			this.checkIsExistPromotion();
			
			// 设置活动时间
			Map<String,Object> PromotionMap = new HashMap<String,Object>();
			PromotionMap.put("PromotionInfo->PromotionBeginTime",YMTDateUtil.getBeforeOrNextDay(1));
			PromotionMap.put("PromotionInfo->PromotionEndTime",YMTDateUtil.getBeforeOrNextDay(3));
			JSONObject retBea = new CreateSellerPromotionCall().callAndGetReturnData(PromotionMap);
			
			// 促销商品ID
			int  promotionId = retBea.getJSONObject("Data").getInt("PromotionId");
			// 买手ID
			int SellerId = retBea.getJSONObject("JsonBeanRequest").getInt("SellerId");
			
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",SellerId);
			map1.put("PromotionId",promotionId);
			map1.put("OperatorType",2);
				
			
			// 取消或者提前结束促销活动
			JSONObject retBea1 = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			int code =retBea1.getInt("Code"); // 响应Code
			boolean flag = retBea1.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea1.getJSONObject("Data").getString("Msg");
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动提前结束失败", msg, "促销活动提前结束失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CancelOrEndPromotion_0042() {
		Logger.start(true, "提前结束促销活动失败，活动不存在");
		try {
			//请求参数
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("SellerId",3383);
			map1.put("PromotionId",99999);
			map1.put("OperatorType",2);
			
			// 取消或者提前结束促销活动
			JSONObject retBea = new CancelOrEndPromotionCall().callAndGetReturnData(map1);
			int code =retBea.getInt("Code"); // 响应Code
			boolean flag = retBea.getJSONObject("Data").getBoolean("IsSuccess"); // 成功标识 
			String msg = retBea.getJSONObject("Data").getString("Msg");
			
			// 断言
			Logger.verifyEquals(200, code, "code是否等于200");
			Logger.verifyEquals(false, flag, "false");
			Logger.verifyEquals("促销活动提前结束失败", msg, "促销活动提前结束失败");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/*
	 * 取消已经存在的满减促销活动
	 */
	public void checkIsExistPromotion() throws InterruptedException, JSONException{
		// 校验是否存在未开始或已开始的满减活动，若存在，则取消
		List<YmtSellerPromotion> sellerPromotionIDlist = new YmtSellerPromotionIMapper().selectPromotionBySellerId(20323743);
		if(sellerPromotionIDlist.size() > 0){
			for(int i=0;i<sellerPromotionIDlist.size();i++){
				Map<String,Object> promotionIdmap= new HashMap<String,Object>();
				promotionIdmap.put("PromotionId",sellerPromotionIDlist.get(i).getPromotionId());
				promotionIdmap.put("SellerId",20323743);
				if(sellerPromotionIDlist.get(i).getPromotionState() == 0){
					promotionIdmap.put("OperatorType",1);
				} else{
					promotionIdmap.put("OperatorType",2);
				}
				
				JSONObject CancelOrEndPromotionretBea = new CancelOrEndPromotionCall().callAndGetReturnData(promotionIdmap);
				boolean flag = CancelOrEndPromotionretBea.getJSONObject("Data").getBoolean("IsSuccess");
				Logger.verifyEquals(true, flag, "验证促销活动是否成功取消");
				
				int code =CancelOrEndPromotionretBea.getInt("Code");
				Logger.verifyEquals(200, code, "code是否等于200");
			}
		}else{
		}
		
	}
	

}
